package com.dhh.web.modules.menu.service.impl;

import com.dhh.web.common.exception.BusinessException;
import com.dhh.web.common.utils.RandomUtil;
import com.dhh.web.modules.menu.domain.Menu;
import com.dhh.web.modules.menu.repo.MenuRepo;
import com.dhh.web.modules.menu.service.MenuService;
import com.dhh.web.modules.user.domain.Perms;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Auther: dhh
 * @Date: 2018/11/27 17:23
 * @Email: fzlinjianhui@163.com
 * @Description:
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuRepo menuRepo;

    @Override
    public List<Menu> findTopMenuByPerms(Set<Perms> perms) {
        return menuRepo.findAllByPermsInAndPidIsOrderByOrderNum(perms,"0");
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuRepo.findAll();
    }

    @Override
    public void save(Menu menu) {
        if(StringUtils.isEmpty(menu.getMenuId())){
            menu.setMenuId(RandomUtil.randomUUID());
        }
        if(menu.getOrderNum() == null){
            menu.setOrderNum(0);
        }
        if(StringUtils.isEmpty(menu.getIcon())){
            menu.setIcon("layui-icon-set");
        }
        if(StringUtils.isEmpty(menu.getUrl())){
            menu.setUrl("javascript:;");
        }
        if(StringUtils.isEmpty(menu.getPid())){
            menu.setPid("0");
        }
        menuRepo.save(menu);
    }

    @Override
    public void delete(String menuId) {
        if(StringUtils.isEmpty(menuId)){
            throw new BusinessException("菜单id不能为空");
        }
        menuRepo.deleteById(menuId);
    }
}
